﻿using System;
using System.Collections.Generic;
using System.Text;
using StockTrader.BusinessService.DataContract.HnxInfo;
using StockTrader.DAL.HnxMonitor.DataAccess;
using StockTrader.BusinessService.DataContract.Global;

namespace StockTrader.DAL.HnxMonitor.BusinessLogic
{
    public static class DataEngine
    {
        public static List<HnxOrdersData> orders = new List<HnxOrdersData>();
        public static List<HnxStockData> stocks = new List<HnxStockData>();
        public static List<HnxMarketData> markets = new List<HnxMarketData>();
        public static List<HnxTopPriceData> topprices = new List<HnxTopPriceData>();

        public static string lastOrderTime = " ";
        public static string lastDealTime = " ";

        public static string Initialize()
        {
            markets.Clear();
            stocks.Clear();
            topprices.Clear();
            orders.Clear();
            lastOrderTime = string.Empty;
            lastDealTime = string.Empty;
            return ProcessStockInfo();
        }

        public static string ProcessOrders()
        {
            DALclient client = new DALclient();
            List<HnxOrdersData> srcOrders = client.getOHnxLastOrders(lastOrderTime);
            List<HnxOrdersData> newOrders = new List<HnxOrdersData>();

            foreach (HnxOrdersData order in srcOrders)
                if (!orders.Contains(order))
                {
                    if (order.OrderTime.CompareTo(lastOrderTime) > 0)
                        lastOrderTime = order.OrderTime;

                    newOrders.Add(order);
                    orders.Add(order);
                }

            foreach (HnxOrdersData order in newOrders)
                client.insertHnxNewOrder(order);

            return newOrders.Count.ToString();
        }

        public static string ProcessStockInfo()
        {
            DALclient client = new DALclient();
            List<HnxStockData> srcStockInfo = client.getOHnxAllStockInfo();
            List<HnxStockData> newStockInfo = new List<HnxStockData>();

            foreach (HnxStockData stock in srcStockInfo)
                if (!stocks.Contains(stock))
                {
                    newStockInfo.Add(stock);
                    stocks.Add(stock);
                    client.insertNewStocksInfo(new StocksInfoData(stock.StockId, stock.Code, stock.Code, 1, "vi", "HNX"));
                }

            foreach (HnxStockData stock in newStockInfo)
            {
                client.insertHnxNewStockInfo(stock);
                if (!stock.MatchPrice.Equals(0))
                    client.insertHnxNewLe(stock);
            }

            return newStockInfo.Count.ToString();
        }

        public static string ProcessTopPrice()
        {
            DALclient client = new DALclient();
            List<HnxTopPriceData> srcTopPrice = client.getOHnxAllTopPrice();
            List<HnxTopPriceData> newTopPrice = new List<HnxTopPriceData>();

            foreach (HnxTopPriceData price in srcTopPrice)
                if (!topprices.Contains(price))
                {
                    newTopPrice.Add(price);
                    topprices.Add(price);
                }

            foreach (HnxTopPriceData price in newTopPrice)
                client.insertHnxNewTopPrice(price);

            return newTopPrice.Count.ToString();
        }

        public static string ProcessMarketInfo()
        {
            DALclient client = new DALclient();
            HnxMarketData srcMarket = client.getOHnxAllMarketInfo();
            List<HnxMarketData> newMarket = new List<HnxMarketData>();

            if (srcMarket != null)
            {
                if (!markets.Contains(srcMarket))
                {
                    newMarket.Add(srcMarket);
                    markets.Add(srcMarket);
                }

                foreach (HnxMarketData market in newMarket)
                    client.insertHnxNewMarketInfo(market);

                return srcMarket.Time.ToString();
            }
            else
                return markets[markets.Count - 1].Time.ToString();
        }

    }
}
